package org.urchr.beorganized.mobile.base;

import java.util.Random;

import org.apache.wicket.Component;
import org.apache.wicket.Page;
import org.apache.wicket.Request;
import org.apache.wicket.Response;
import org.apache.wicket.Session;
import org.apache.wicket.authorization.Action;
import org.apache.wicket.authorization.IAuthorizationStrategy;
import org.apache.wicket.protocol.http.WebApplication;
import org.urchr.beorganized.mobile.pages.login.LoginPage;
import org.urchr.beorganized.mobile.pages.secured.agenda.AgendaPage;
import org.urchr.beorganized.mobile.pages.secured.base.SecuredPage;
import org.urchr.beorganized.mobile.pages.secured.browse.BrowsePage;
import org.urchr.beorganized.mobile.pages.secured.edit.EditPage;
import org.urchr.beorganized.mobile.pages.secured.starred.StarredPage;

public class Application extends WebApplication{

	public static final int TYPE_TEXT = 0;
	public static final int TYPE_IMAGE = 1;
	public static final int TYPE_AUDIO = 2;
	public static final int TYPE_TASK = 3;
	public static final int TYPE_EVENT = 4;
	public static final int TYPE_BMARK = 5;
	public static final int TYPE_FILE = 6;
	
//	public static final String DEFAULT_HOST = "10.0.1.21:8080/BeOrg";
//	public static final String DEFAULT_USER = "kostya";
//	public static final String IMG_PREFIX = "/BeOrg/im/";
	public static final String DEFAULT_HOST = "beorganized.org";
	public static final String DEFAULT_USER = "";
	public static final String IMG_PREFIX = "/im/";
	
	@Override
	protected void init() {
		super.init();
		mountBookmarkablePage("/l", LoginPage.class);
		mountBookmarkablePage("/b", BrowsePage.class);
		mountBookmarkablePage("/a", AgendaPage.class);
		mountBookmarkablePage("/s", StarredPage.class);
		mountBookmarkablePage("/e", EditPage.class);
		
		getSecuritySettings().setAuthorizationStrategy(new IAuthorizationStrategy(){

			public boolean isActionAuthorized(Component arg0, Action arg1) {
				return true;
			}
			
			public <T extends Component> boolean isInstantiationAuthorized(
					Class<T> arg0) {
				MobileSession session = (MobileSession) MobileSession.get();
				if(SecuredPage.class.isAssignableFrom(arg0)){
					return session.isAuthorized();//Check session
				}
				return true;
			}
			
		});
	}
	
	@Override
	public Session newSession(Request request, Response response) {
		return new MobileSession(request);
	}
	
	@Override
	public Class<? extends Page> getHomePage() {
		return LoginPage.class;
	}

	private static Random random = new Random();
	
	public static String genRandomString(){
		return Long.toString(Math.abs(random.nextLong()), 36);
	}

}
